package com.cynteka.cyfoman.ui.collection;

import com.cynteka.cyfoman.core.ComplexPropertyResolver;
import com.cynteka.cyfoman.core.IPropertyResolver;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Sergey
 * Date: 04.08.11
 * Time: 18:01
 * To change this template use File | Settings | File Templates.
 */
public class ColorFontChooserPropertyResolver implements IPropertyResolver {
    public static final String FONT_PN = "___________f_o_n_t";
    public static final String FONT_STYLE_PN = "___________f_o_n_t___s_t_y_l_e";
    public static final String COLOR_PN = "___________c_o_l_o_r";
    public static final String BACKGROUND_COLOR_PN = "___________b_g_c_o_l_o_r";

    public static final String BOLD_FONT_STYLE = "BOLD";
    public static final String ITALIC_FONT_STYLE = "ITALIC";
//    public static final String UNDERSCORE_FONT_STYLE = "UNDERSCORE";


    private IPropertyResolver baseResolver = new ComplexPropertyResolver();
    private Map<String, Font> fontMap;
    private Map<String, String> fontStyleMap;
    private Map<String, Color> colorMap;
    private Map<String, Color> backgroundColorMap;
    private Map<String, Delegator> delegators = new HashMap<String, Delegator>();
    private String selectorProperty;

    public Map<String, String> getFontStyleMap() {
        return fontStyleMap;
    }

    public void setFontStyleMap(Map<String, String> fontStyleMap) {
        this.fontStyleMap = fontStyleMap;
    }

    public String getSelectorProperty() {
        return selectorProperty;
    }

    public void setSelectorProperty(String selectorProperty) {
        this.selectorProperty = selectorProperty;
    }

    public ColorFontChooserPropertyResolver() {
        delegators.put(FONT_PN, new Delegator() {
            @Override
            public Map getMap() {
                return fontMap;
            }
        });
        delegators.put(BACKGROUND_COLOR_PN, new Delegator() {
            @Override
            public Map getMap() {
                return backgroundColorMap;
            }
        });
        delegators.put(COLOR_PN, new Delegator() {
            @Override
            public Map getMap() {
                return colorMap;
            }
        });
        delegators.put(FONT_STYLE_PN, new Delegator() {
            @Override
            public Map getMap() {
                return fontStyleMap;
            }
        });
    }

    public IPropertyResolver getBaseResolver() {
        return baseResolver;
    }

    public void setBaseResolver(IPropertyResolver baseResolver) {
        this.baseResolver = baseResolver;
    }

    public Map<String, Font> getFontMap() {
        return fontMap;
    }

    public void setFontMap(Map<String, Font> fontMap) {
        this.fontMap = fontMap;
    }

    public Map<String, Color> getColorMap() {
        return colorMap;
    }

    public void setColorMap(Map<String, Color> colorMap) {
        this.colorMap = colorMap;
    }

    public Map<String, Color> getBackgroundColorMap() {
        return backgroundColorMap;
    }

    public void setBackgroundColorMap(Map<String, Color> backgroundColorMap) {
        this.backgroundColorMap = backgroundColorMap;
    }

    @Override
    public Object getProperty(Object object, String propertyName) {
        Delegator delegator = delegators.get(propertyName);
        if(delegator != null) {
            String selectorValue = String.valueOf(baseResolver.getProperty(object, selectorProperty));
            Object res = delegator.get(selectorValue);
            if(FONT_STYLE_PN.equals(propertyName) && res != null) {
                res = getFontStyle(String.valueOf(res));
            }
            return res;
        } else {
            return baseResolver.getProperty(object, propertyName);
        }
    }

    private abstract class Delegator {

        protected abstract Map getMap();

        public Object get(String key) {
            Map map = getMap();
            return map == null ? null : map.get(key);
        }
    }

    private int getFontStyle(String s) {
        String[] styles = s.split(" ");
        int res = 0;
        for(String style : styles) {
            if(BOLD_FONT_STYLE.equalsIgnoreCase(style)) {
                res |= Font.BOLD;
            } else if(ITALIC_FONT_STYLE.equalsIgnoreCase(style)) {
                res |= Font.ITALIC;
            }
        }
        return res;
    }
}
